package com.mokio.service.impl;

import com.mokio.entity.Users;
import com.mokio.entity.UsersExample;
import com.mokio.mapper.UsersMapper;
import com.mokio.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: xiaomishop
 * @ClassName UserServiceImpl
 * @description:
 * @author: mokio
 * @create: 2024−12-10 21:32
 * @Version 1.0
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UsersMapper usersMapper;

    @Override
    public Map<String, Object> login(String username, String password) {
        Map<String, Object> map = new HashMap<>();

        //使用对象
        UsersExample example = new UsersExample();
        //使用条件对象
        example.createCriteria().andUnameEqualTo(username);
        //得到集合
        List<Users> usersList = usersMapper.selectByExample(example);

        if(usersList!=null && usersList.size()>0){
            // 集合中必然有数据
            Users users = usersList.get(0);
            //比对密码
            if(password.equals(users.getUpass())){
                //密码也相等
                map.put("user",users);
                map.put("s","ok");
            }else{
                map.put("s","notok"); //success  error

            }
        }


        return map;
    }

    @Override
    public Users findUserByName(String username) {
        UsersExample example = new UsersExample();
        example.createCriteria().andUnameEqualTo(username);
        List<Users> users = usersMapper.selectByExample(example);
        if(users!=null && users.size()>0){
            return users.get(0);
        }
        return null;
    }

    @Override
    public void saveUser(Users newUser) {
        usersMapper.insert(newUser);
    }
}